{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "## Create a Portfolio with GS Quant\n",
    "\n",
    "**First define your positions and risk model:**\n",
    "\n",
    "*We will resolve all identifiers (Bloomberg IDs, SEDOLs, RICs, etc) historically on our end as of the position date*"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "import datetime as dt\n",
    "import warnings\n",
    "\n",
    "from gs_quant.markets.portfolio import Portfolio\n",
    "from gs_quant.markets.portfolio_manager import PortfolioManager\n",
    "from gs_quant.markets.position_set import Position, PositionSet\n",
    "from gs_quant.markets.report import FactorRiskReport\n",
    "from gs_quant.session import GsSession, Environment\n",
    "from gs_quant.common import PositionTag\n",
    "\n",
    "GsSession.use(Environment.PROD)\n",
    "warnings.filterwarnings(\"ignore\", category=RuntimeWarning)\n",
    "\n",
    "portfolio_position_sets = [\n",
    "    PositionSet(\n",
    "        date=dt.date(day=3, month=5, year=2022),\n",
    "        positions=[\n",
    "            Position(identifier='AAPL UW', quantity=25, tags=[PositionTag(name='Analyst', value='Marcus Goldman')]),\n",
    "            Position(identifier='GS UN', quantity=50, tags=[PositionTag(name='Analyst', value='Samuel Sachs')])\n",
    "        ]\n",
    "    ),\n",
    "    PositionSet(\n",
    "        date=dt.date(day=1, month=7, year=2022),\n",
    "        positions=[\n",
    "            Position(identifier='AAPL UW', quantity=26, tags=[PositionTag(name='Analyst', value='Marcus Goldman')]),\n",
    "            Position(identifier='GS UN', quantity=51, tags=[PositionTag(name='Analyst', value='Samuel Sachs')])\n",
    "        ]\n",
    "    )\n",
    "]\n",
    "\n",
    "for pos_set in portfolio_position_sets:\n",
    "    pos_set.resolve()\n",
    "\n",
    "risk_model_id = 'BARRA_USFAST'\n",
    "\n",
    "print('Positions and risk model ID saved.')"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "**Now, we'll create a new empty portfolio...**"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "portfolio = Portfolio(name='My New Portfolio')\n",
    "portfolio.save(overwrite=True)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "**And update your portfolio with the positions you specified earlier:**"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "pm = PortfolioManager(portfolio.id)\n",
    "pm.update_positions(portfolio_position_sets)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "**Now you can create a factor risk report for your portfolio...**"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "risk_report = FactorRiskReport(risk_model_id=risk_model_id, fx_hedged=True)\n",
    "risk_report.set_position_source(portfolio.id)\n",
    "risk_report.save()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "**And schedule all portfolio reports to begin calculating analytics:**"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "pm.schedule_reports()\n",
    "\n",
    "print(f'Check out your new portfolio in Marquee! View it here: https://marquee.gs.com/s/portfolios/{portfolio.id}/summary')"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}